package com.daylive.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;





import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.daylive.model.Agent;
import com.daylive.page.Page;
import com.daylive.service.AgentService;


@Controller
@RequestMapping("agent")
public class AgentController {
	private final Logger logger = Logger.getLogger(AgentController.class);
	
	@Autowired
	private AgentService agentService;
	public static HttpSession getSession() {
		HttpSession session = null;
		try {
			session = getRequest().getSession();
		} catch (Exception e) {
		}
		return session;
	}

	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		return attrs.getRequest();
	}
	private void clearSession() {
		getSession().invalidate();
	}

	@RequestMapping("/agent_list.do")
	public ModelAndView getAgentList() {
		return getAgentList(null,1);
	}
	@RequestMapping("/agent_page_list.do")
	public ModelAndView getAgentList(@RequestParam("companyName") String companyName,@RequestParam int pageNumber) {
		Page<Agent> pageList = agentService.getAgentList(companyName, pageNumber);
		ModelAndView mv = new ModelAndView("agent_list");
		mv.addObject("currentPage", pageNumber);
		mv.addObject("companyName", companyName);
		mv.addObject("totalPage", pageList.getTotalPage());
		mv.addObject("totalCount", pageList.getTotalCount());
		mv.addObject("agentList", pageList.getResultList());
		return mv;
	}
	 @RequestMapping("/agent_add.do")
		public ModelAndView addAgent() {
			ModelAndView mv = new ModelAndView();
			mv.setViewName("agent_add");
			return mv;
		}
	   @RequestMapping("/agent_save.do")
	   	public ModelAndView saveAgent(Agent agent) {
	   		ModelAndView mv = new ModelAndView();
	   		int rel = agentService.addAgent(agent);
	   		mv.setViewName("success");
			mv.addObject("msg",rel > 0 ? "数据添加成功！" : "数据添加失败！");
			return mv;   		
	   	}
	  @RequestMapping("/agent_edit.do")
		public ModelAndView editAgent(@RequestParam("id")int id) {
		  Agent agent = agentService.getAgentById(id);
			ModelAndView mv = new ModelAndView();
			mv.addObject("agent", agent);
			mv.setViewName("agent_edit");
			return mv;
		}
	    @RequestMapping("/agent_update.do")
	   	public ModelAndView updateAgent(Agent agent) {
	   		ModelAndView mv = new ModelAndView();
	   		int rel =agentService.updateAgent(agent);
	   		mv.setViewName("success");
			mv.addObject("msg",rel > 0 ? "数据修改成功！" : "数据修改失败！");
			return mv;   		
	   	}
	    @RequestMapping("/agent_del.do")
		public String deleteAgent(@RequestParam("id")int id) {
			agentService.delAgent(id);
			return "success";
		}
		@RequestMapping("/agent_delAll.do")
		public String deleteAll(String ids) {
			String[] array_IDS = ids.split(",");
			agentService.delAll(array_IDS);
			return "success";
		}
		/**
		 * check_user
		 * @return
		 *//*
		@RequestMapping("/user_check.do")
		@ResponseBody
	    public String checkUser(@RequestParam String usersName) {  
			try {
				usersName = new String(usersName.getBytes("ISO-8859-1"),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			Users user = userService.getUsersName(usersName);
			return null==user ? "0" : "1";
		}*/
}